import 'package:flutter/material.dart';

class GameSubscriptionDataItem {
  late String id;
  late String eventName;
  late String unitName;
  late String disciplineName;
  late String status;
  late String statusName;
  late String code;
  late String venueName;
  late String cover;
  late String background;
  late String date;
  late String time;
  late String week;
  late bool subscribed;
  late String scheduleName;
  late int videoType; // 0 无关联 1直播 2点播

  GameSubscriptionDataItem(Map<String, dynamic> json) {
    id = "${json["id"]}";
    eventName = "${json["event_name"]}";
    disciplineName = "${json["discipline_name"]}";
    unitName = "${json["unit_name"]}";
    status = "${json["status"]}";
    statusName = "${json["status_name"]}";
    code = "${json["code"]}";
    venueName = "${json["venue_name"]}";
    date = "${json["date"]}";
    time = "${json["time"]}";
    week = "${json["week"]}";
    cover = "${json["cover"]}";
    background = "${json["background"]}";
    try {
      subscribed = json["subscribed"] as bool;
    } catch (e) {
      subscribed = false;
    }
    try {
      videoType = json["game_video_type"] as int;
    } catch (e) {
      videoType = 0;
    }
    scheduleName = "${json["schedule_name"]}";
  }
  Map<String, dynamic> get json {
    return Map<String, dynamic>()
      ..["id"] = id
      ..["event_name"] = eventName
      ..["unit_name"] = unitName
      ..["discipline_name"] = disciplineName
      ..["status"] = status
      ..["status_name"] = statusName
      ..["code"] = code
      ..["venue_name"] = venueName
      ..["date"] = "date"
      ..["time"] = time
      ..["week"] = week
      ..["cover"] = cover
      ..["background"] = background
      ..["subscribed"] = subscribed
      ..["schedule_name"] = scheduleName
      ..["game_video_type"] = videoType;
  }
}

class GameSubscriptionData {
  late List<GameSubscriptionDataItem> list;
  late String currentDate;

  GameSubscriptionData(Map<String, dynamic> json) {
    try {
      list = (json["list"] as List<dynamic>)
          .map((e) => GameSubscriptionDataItem(e))
          .toList();
    } catch (e) {
      print(e.toString());
    }

    currentDate = json["current_date"];
  }
}

class GameSubcriptionManageDisciplineItem {
  late String id;
  late String discipline;
  late String disciplineName;
  GameSubcriptionManageDisciplineItem(Map<String, dynamic> json) {
    id = "${json["id"]}";
    discipline = "${json["discipline"]}";
    disciplineName = "${json["discipline_name"]}";
  }
  Map<String, dynamic> get json {
    return Map<String, dynamic>()
      ..["id"] = id
      ..["discipline"] = discipline
      ..["discipline_name"] = disciplineName;
  }
}

class GameSubcriptionManageVenueItem {
  late String id;
  late String code;
  late String venueName;
  GameSubcriptionManageVenueItem(Map<String, dynamic> json) {
    id = "${json["id"]}";
    code = "${json["code"]}";
    venueName = "${json["name"]}";
  }
  Map<String, dynamic> get json {
    return Map<String, dynamic>()
      ..["id"] = id
      ..["code"] = code
      ..["name"] = venueName;
  }
}

class GameSubcriptionManageData extends ChangeNotifier {
  static GameSubcriptionManageData model = GameSubcriptionManageData._();
  late List<GameSubcriptionManageDisciplineItem> discipline;
  late List<GameSubcriptionManageVenueItem> venue;

  GameSubcriptionManageData._() {
    initValues();
  }

  initValues() {
    this.discipline = [];
    this.venue = [];
  }

  GameSubcriptionManageData(Map<String, dynamic> json) {
    this.discipline = (json["discipline"] as List<dynamic>)
        .map((e) => GameSubcriptionManageDisciplineItem(e))
        .toList();
    this.venue = (json["venue"] as List<dynamic>)
        .map((e) => GameSubcriptionManageVenueItem(e))
        .toList();
  }

  static load(Map<String, dynamic> json) {
    model.discipline = (json["discipline"] as List<dynamic>)
        .map((e) => GameSubcriptionManageDisciplineItem(e))
        .toList();
    model.venue = (json["venue"] as List<dynamic>)
        .map((e) => GameSubcriptionManageVenueItem(e))
        .toList();
    model.notifyListeners();
    return GameSubcriptionManageData(json);
  }

  static add(Map<String, dynamic> json, String type) {
    if (type == 'discipline') {
      model.discipline.insert(0, GameSubcriptionManageDisciplineItem(json));
    } else if (type == 'venue') {
      model.venue.insert(0, GameSubcriptionManageVenueItem(json));
    }
    model.notifyListeners();
  }

  static remove(String code, String type) {
    if (type == 'discipline') {
      model.discipline.removeWhere((e) => e.discipline == code);
    } else if (type == 'venue') {
      model.venue.removeWhere((e) => e.code == code);
    }
    model.notifyListeners();
  }
}
